package com.alvin.abp.web.controller;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.alvin.abp.common.context.CurrentUserContext;
import com.alvin.abp.domain.AlvinUser;
import com.alvin.abp.domain.Bonus;
import com.alvin.abp.domain.Record;
import com.alvin.abp.domain.Rule;
import com.alvin.abp.domain.editor.EntityPropertyEditor;
import com.alvin.abp.domain.validator.BonusValidator;
import com.alvin.abp.service.BonusService;
import com.alvin.abp.service.RuleService;
import com.alvin.abp.service.UserService;

@Controller
public class RecordController {
	@Autowired
	private UserService userService;
	@Autowired
	private RuleService ruleService;
	@Autowired
	private BonusService bonusService;
	@Autowired
	private BonusValidator bonusValidator;
	
	protected final Log logger = LogFactory.getLog(getClass());
	
	@InitBinder
	public void initBinder(WebDataBinder binder) {
		binder.registerCustomEditor(AlvinUser.class, new EntityPropertyEditor(AlvinUser.class));
		binder.registerCustomEditor(Rule.class, new EntityPropertyEditor(Rule.class));
	}
	
	@RequestMapping(value = "/records.htm", method = RequestMethod.GET)
	public String recordsForm(@RequestParam("id") Long id, ModelMap model) {
		AlvinUser user = userService.readUserById(id);
		List<Record> records = userService.readRecords(id);
		user.setRecords(records);
		model.addAttribute("user", user);
		return "user/records";
	}
	
	@RequestMapping(value="/collect.htm", method = RequestMethod.GET)
	public String collectForm(@RequestParam(value="status", required=false) String status, ModelMap model){
		List<Rule> rules = ruleService.readRules();
		AlvinUser user = userService.readUserById(CurrentUserContext.getCurrentUser().getId());
		Bonus bonus = new Bonus();
		model.addAttribute("rules", rules);
		model.addAttribute(bonus);
		model.addAttribute("status", status);
		model.addAttribute("user", user);
		return "home";
	}
	
	@RequestMapping(value="/collect.htm", method = RequestMethod.POST)
	public String collectPoint(@ModelAttribute("bonus") Bonus bonus, Errors errors, ModelMap model) {
		ValidationUtils.invokeValidator(bonusValidator, bonus, errors);
		
		List<Rule> rules = ruleService.readRules();
		model.addAttribute("rules", rules);
		model.addAttribute(bonus);
		AlvinUser user = userService.readUserById(CurrentUserContext.getCurrentUser().getId());
		model.addAttribute("user", user);
		
		if(errors.hasErrors()) {
			model.addAllAttributes(errors.getAllErrors());
			return "home";
		}else{
			bonusService.addBonus(bonus);
			return "redirect:/collect.htm?status=success";
		}
	}
}
